perm filename PART12.TEX[TEX,RWF]1 blob sn#534458 filedate 1980-09-19 generic text, type C, neo UTF8
COMMENT āŠ—   VALID 00004 PAGES
C REC  PAGE   DESCRIPTION
C00001 00001
C00002 00002	%After indefinite iteration
C00004 00003	\sample
C00006 00004	The program above will always halt to prove it takes a series of steps.
C00008 ENDMK
CāŠ—;
%After indefinite iteration
\sendnotes{After indefinite iteration or in sample prog section.}
\sample
\example
Suppose  $f$ is a continuous function, and  $f(0) > 0$, but for sufficiently
large $x$, $f(x) < 0$.  This program will find, within .000001, a root of
$f(x) = 0$.

\startcode
PROGRAM ROOTFIND ;

VAR     R, STEP : REAL;

BEGIN 

(* FIND FIRST INTEGER R FOR WHICH :
   F( R-1 ) > 0,  F( R ) <= 0  *)

R := 1.0 ;
WHILE F( R ) > 0.0 DO  R := R + 1 ;
STEP := 0.5 ;

WHILE  STEP > 0.000001  DO   
    BEGIN
    IF  F( R-STEP ) <= 0.0  THEN 
       R := R-STEP ;

    (* ROOT LIES BETWEEN R AND R-STEP *)
    STEP := STEP / 2.0
    END;

WRITELN ( R-STEP )
END.
\endcode
\sample
\example
Assume  $f(x)$  is a continuous function which is negative for some range of
values of $x$, possibly only with  $x$  very large.  We want to find an  $x$
for which $f(x) < 0$.

\startcode
PROGRAM MAKENEG ;

VAR     P, X, STEP :  REAL ;

BEGIN
X := 0.0 ;
P := 1.0 ;

WHILE  F( X ) >= 0.0  DO
    BEGIN
    STEP := 1.0 / P ;
    X :=  - P + STEP / 2.0 ;
    WHILE ( F(X) >= 0.0 ) AND ( X < P ) DO
        X := X + STEP ;

    (* EITHER F(X) < 0 NOW, OR WE HAVE LOOKED
       FROM  - P TO P BY STEPS OF 1/P  *)

    P := 2.0 * P
    END;

(* NOW F(X) < 0  *)
WRITELN ( X ) 
END.

\endcode
The program above will always halt; to prove it takes a series of steps.

\bitem {\tt P} starts at 1, and only changes by doubling, so it is always
positive.

\bitem {\tt STEP} is assigned only {\tt 1/P}, so it is always positive.

\bitem In the inner iteration, $x$ keeps getting increased by the same
positive amount {\tt STEP}; {\tt P} doesn't change.  Eventually, $x$ must
become greater than {\tt P}, so the inner iteration eventually halts.

\bitem If the program did not halt, the outer iteration would be executed an
infinite number of times.  In the process, {\tt P} would get larger than any
given number, and {\tt STEP} would get smaller than any given number.  The
inner iteration eventually tries an $x$ in every range of values no matter how
remote and small.  When it tries one in the range where $f(x) < 0$, it makes
no further change in $x$, and both iterations terminate.